<template>
  <a-list :class="prefixCls" :pagination="paginationConfig">
    <template v-for="item in tabDataList" :key="item.id">
      <a-list-item class="list-item" @click="handleTitleClick(item)" :style="{ cursor: isTitleClickable ? 'pointer' : '' }">
        <a-list-item-meta>
          <template #title>
            <div class="title">
              <a-typography-paragraph style="width: 100%; margin-bottom: 0 !important" :delete="!!item.titleDelete" :ellipsis="$props.titleRows && $props.titleRows > 0 ? { rows: $props.titleRows, tooltip: !!item.title } : false" :content="item.title" />
              <div class="extra" v-if="item.priority">
                <a-tag class="tag" color="Red" v-if="item.priority === PriorityTypes.H">
                  {{ item.priority }}
                </a-tag>
                <a-tag class="tag" color="Orange" v-if="item.priority === PriorityTypes.M">
                  {{ item.priority }}
                </a-tag>
                <a-tag class="tag" color="Gray" v-if="item.priority === PriorityTypes.L">
                  {{ item.priority }}
                </a-tag>
              </div>
            </div>
          </template>

          <template #avatar>
            <a-avatar v-if="item.msgCategory === MsgTypes.BUSINESS" class="avatar" src="https://gw.alipayobjects.com/zos/rmsportal/OKJXDXrmkNshAMvwtvhu.png" />
            <a-avatar v-else-if="item.msgCategory === MsgTypes.CHAT" class="avatar" src="https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg" />
            <a-avatar v-else-if="item.msgCategory === MsgTypes.SYS" class="avatar" src="https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png" />
            <template v-else>
              <a-avatar class="avatar priority-L" title="未知消息">
                <template #icon>
                  <Icon icon="entypo:info" />
                </template>
              </a-avatar>
            </template>
          </template>

          <template #description>
            <div>
              <div class="description" v-if="item.msgContent">
                <a-typography-paragraph style="width: 100%; margin-bottom: 0 !important" :ellipsis="$props.descRows && $props.descRows > 0 ? { rows: $props.descRows, tooltip: !!item.msgContent } : false" :content="item.msgContent" />
              </div>
              <div class="datetime">
                <Time :value="item.sendTime" :title="item.sendTime" />
              </div>
            </div>
          </template>
        </a-list-item-meta>
      </a-list-item>
    </template>
  </a-list>
</template>
<script lang="ts">
import { computed, defineComponent, PropType, ref, watch, unref, onMounted } from 'vue';
import { useDesign } from '/@/hooks/web/useDesign';
import { List, Avatar, Tag, Typography } from 'ant-design-vue';
import { Time } from '/@/components/Time';
import { MsgTypes, PriorityTypes, ListItem } from './data';
import { getMessagePage } from '/@/api/common/api';
import { isNumber } from '/@/common/util/is';
import { useMessage } from '/@/hooks/web/useMessage'; // 轻提醒
const { createMessage: $message } = useMessage(); // 解构

export default defineComponent({
  components: {
    [Avatar.name]: Avatar,
    [List.name]: List,
    [List.Item.name]: List.Item,
    AListItemMeta: List.Item.Meta,
    ATypographyParagraph: Typography.Paragraph,
    [Tag.name]: Tag,
    Time,
  },
  props: {
    list: {
      type: Array as PropType<ListItem[]>,
      default: () => [],
    },
    pageSize: {
      type: [Boolean, Number] as PropType<Boolean | Number>,
      default: 5,
    },
    currentPage: {
      type: Number,
      default: 1,
    },
    titleRows: {
      type: Number,
      default: 1,
    },
    descRows: {
      type: Number,
      default: 1,
    },
    onTitleClick: {
      type: Function as PropType<(Recordable) => void>,
    },
    msgCategory: {
      type: String,
      default: '0',
    },
  },
  setup(props, { emit }) {
    const { prefixCls } = useDesign('header-notify-list');

    const tabDataList = ref(props.list || []);
    //分页相关
    const pageNo = ref(1);
    const pageSize = ref(5);
    const total = ref(0);
    const paginationConfig = ref({
      showSizeChanger: false,
      pageSize,
      current: pageNo,
      total,
      // size: 'small',
      responsive: true,
      showQuickJumper: false,
      showTotal: (total) => `总 ${total} 条`,
      onChange: handlePageChange,
      // onShowSizeChange: handlePageSizeChange,
    });

    onMounted(() => {
      //初始化
      loadData();
    });

    // 获取消息列表
    async function loadData() {
      let params = {
        pageNo: pageNo.value,
        pageSize: pageSize.value,
        msgCategory: props.msgCategory,
        readFlag: 0,
        authFlag: 1,
      }
      let data = await getMessagePage(params);
      tabDataList.value = data.records;
      total.value = data.total; // 更新总条数
    }

    // 页码变化时触发
    function handlePageChange(p, pz) {
      pageNo.value = p;
      pageSize.value = pz;
      loadData(); // 重新请求数据
    }

    //跳转到指定页面
    function handlePageSizeChange(current, size) {
      pageSize.value = size;
      loadData();
    }

    watch(
      () => props.msgCategory,
      (v) => {
        //切换tab时，需要重置页数
        pageNo.value = 1;
        // $message.info('测试2222......'+v);
        loadData();
      }
    );

    //判断是否显示链接样式 ： 手形状
    const isTitleClickable = computed(() => !!props.onTitleClick);

    // const getPagination = computed(() => {
    //   console.log('11111：44444444444');
    //   const { list, pageSize } = props;
    //   if (pageSize > 0 && list && list.length > pageSize) {
    //     return {
    //       total: list.length,
    //       pageSize,
    //       //size: 'small',
    //       current: unref(current),
    //       onChange(page) {
    //         current.value = page;
    //       },
    //     };
    //   } else {
    //     return false;
    //   }
    // });

    function handleTitleClick(item: ListItem) {
      props.onTitleClick && props.onTitleClick(item);
    }

    return {
      prefixCls,
      tabDataList,
      paginationConfig,
      handleTitleClick,
      isTitleClickable,
      MsgTypes,
      PriorityTypes,
    };
  },
});
</script>
<style lang="less" scoped>
@prefix-cls: ~'@{namespace}-header-notify-list';

.@{prefix-cls} {
  width: 340px;

  &::-webkit-scrollbar {
    display: none;
  }

  :deep(.ant-pagination-disabled) {
    display: inline-block !important;
  }

  &-item {
    padding: 6px;
    overflow: hidden;
    cursor: pointer;
    transition: all 0.3s;

    .title {
      margin-bottom: 8px;
      font-weight: normal;

      .extra {
        float: right;
        margin-top: -1.5px;
        margin-right: 0;
        font-weight: normal;

        .tag {
          margin-right: 0;
        }
      }

      .avatar {
        margin-top: 4px;
      }

      .description {
        font-size: 12px;
        line-height: 18px;
      }

      .datetime {
        margin-top: 4px;
        font-size: 12px;
        line-height: 18px;
      }
    }
  }

  .list-item {

    .priority-L,
    .priority-M,
    .priority-H {
      font-size: 12px;
    }

    .priority-L {
      background-color: #7cd1ff;
    }

    .priority-M {
      background-color: #ffa743;
    }

    .priority-H {
      background-color: #f8766c;
    }

    .description {
      font-size: 12px;
      line-height: 18px;
    }
  }
}
</style>
